/*
 * fonts.c
 *
 *  Created on: 01/12/2009
 *      Author: manu
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <img.h>
#include <math.h>


#define INC 1

int init(imgT ** screen, imgT **  forest, imgT **  buffer,char *forest_file);
void animate(imgT * screen, imgT * forest, imgT * buffer);

int main(int argc, char ** argv)
{
	int fd,i;
	imgT * screen, * forest, * buffer;

	if (init(&screen, &forest, &buffer, "/usr/img/mario.bmp") == 0)
		return 0;

	animate(screen, forest, buffer);

	
	free(forest->image);
	free(forest);
	free(buffer->image);
	free(buffer);

	imgFree(screen);

	return 0;
}

int init(imgT ** screen, imgT ** forest, imgT ** buffer,char *forest_file)
{

	

	if ((*forest = imgLoad(forest_file, IMG_BMP)) == NULL)
	{
		printf("Bad image file, or not enough memory.\n");
		return 0;
	}

	if ((*screen = imgScreen(getResolutionCode(*forest))) == NULL)
	{
		printf("Couldn't create screen, not enough memory.\n");
		return 0;
	}

	return 1;
}

void animate(imgT * screen, imgT * forest, imgT * buffer)
{
	rectT pos, mask, racc, buff, frst;
	int offset = 0;
	int inc = -1;
	int i;
	int flags,aux;
	
	int size;
	
	colorT color = {0xFF, 0xFF, 0xFF};

	/*Initialize some data*/
	
	frst.w = screen->w;
	frst.h = screen->h;
	frst.orig.x = 0;
	frst.orig.y = 0;

	imgBlt(forest, frst, screen, frst, BLT_CPY);
	imgScreenRefresh(screen);
	

	flags = fcntl(0, F_GETFL, 0);

	/*Pongo non block*/
	fcntl(0, F_SETFL, flags | O_NONBLOCK);

	while (read(0,&aux,1)==-1)
	{
		/*Salvar el fondo*/
		imgBlt(screen, pos, buffer, buff, BLT_CPY);
		
		/*Refrescar la pantalla*/
		imgScreenRefresh(screen);
	
		size = 1;
		draw_string(screen, "Presione ENTER dos veces para salir", 400, 10, size, color); 
		for (; size < 5; size++) {
			draw_boxed_string(screen, "Este es un texto encuadrado.", 10, \
				 10+130*(size-1), size, 16, 2, color);
			draw_string(screen, "Bitmap power! ABCDEFGHIJKLMNOPQ"  \
				"RSTUVXYZ0123456789", 10, 85+130*(size-1), size, color); 
		}
		
	}

	/*Pongo block*/
	fcntl(0, F_SETFL, flags);

}
